﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tracy.Handlers;
using Tracy.DataModel;
using SharedLib;
using System.Collections.Concurrent;
using System.Threading;

namespace Tracy.Plugins.DownloadAdapters
{
    public class EmuleNewTaskDialogHandler : TaskMessageHandler
    {
        static Queue<Resource> _cachedTasks = new Queue<Resource>();
        static private object _lockHelper = new object();
        protected override void ProcessMessage(Messages.TaskMessage msg)
        {
            if(msg != null && msg.Data != null && msg.Data is Resource && !msg.CancelFlag)
            {
                Resource res = msg.Data as Resource;
                string defaultPath = res.Properties["default-path"];
                string autoMode = res.Properties["auto-mode"];

                int currentCount = 0;
                lock (_lockHelper)
                {
                    _cachedTasks.Enqueue(res);
                    currentCount = _cachedTasks.Count;
                }

                if (currentCount > 0)
                {
                    const int MAX_WAIT_TIMES_SENDTASK = 6; //wait for 3 sec
                    int wait = 0;
                    bool flag = false;
                    while (wait < MAX_WAIT_TIMES_SENDTASK)
                    {
                        Thread.Sleep(500);
                        lock (_lockHelper)
                        {
                            flag = (currentCount != _cachedTasks.Count);
                        }
                        if (flag) return;
                        if (msg.CancelFlag) return;
                        wait++;
                    }
                }

                lock (_lockHelper)
                {
                    while(_cachedTasks.Count > 0){
                        _cachedTasks.Dequeue();
                    }

                    int dialoghWnd = FindDialog();
                    SetDefaultFolder(dialoghWnd, defaultPath);
                    if (autoMode == "true") {
                        CloseDialog(dialoghWnd); 
                    }
                }
            }
            
        }

        private int FindDialog()
        {
            string emuleDialogTitle = "添加任务";
            const int MAX_RETRY_TIMES_ADDTASK = 1000;  //10 seconds timeout
            int dialoghWnd = 0;
            int retry = 0;

            do
            {
                System.Threading.Thread.Sleep(10);
                dialoghWnd = Win32Helper.FindWindow("#32770", emuleDialogTitle);
                retry++;
                if (retry >= MAX_RETRY_TIMES_ADDTASK) throw new Exception("Dialog \"" + emuleDialogTitle + "\" not found"); //timeout
                //if (killThreadFlag) return;
            } while (dialoghWnd == 0);

            return dialoghWnd;
        }

        private void CloseDialog(int dialoghWnd)
        {
            //Send Enter
            int btnhWnd = Win32Helper.FindWindowEx(dialoghWnd, 0, "Button", "确定");
            Win32Helper.PostMessage(btnhWnd, Win32Helper.WM_KEYDOWN, 13, 0);
        }

        private static void SetDefaultFolder(int dialoghWnd, string defaultPath)
        {
            //input default folder
            int texthWnd = Win32Helper.FindWindowEx(dialoghWnd, 0, "ComboBox", null);
            Win32Helper.SendMessage(texthWnd, Win32Helper.WM_SETTEXT, 0, defaultPath);
        }

    }
}
